package com.jiaming.employee.emp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiaming.employee.common.bean.vo.MyPageVO;
import com.jiaming.employee.common.exception.ApiException;
import com.jiaming.employee.emp.bean.pojo.Emp;
import com.jiaming.employee.emp.bean.vo.AddEmpVO;
import com.jiaming.employee.emp.bean.vo.PageEmpDataVO;
import com.jiaming.employee.emp.bean.vo.PageEmpVO;
import com.jiaming.employee.emp.mapper.EmpMapper;
import com.jiaming.employee.emp.service.IEmpService;
import com.jiaming.employee.salary.bean.pojo.Salary;
import com.jiaming.employee.salary.service.ISalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sun
 * @create 2021-05-14-21:09
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements IEmpService {

    @Autowired
    ISalaryService salaryService;

    @Override
    public MyPageVO<PageEmpDataVO> pageEmp(PageEmpVO pageEmpVO) {

        MyPageVO<PageEmpDataVO> pageVO = new MyPageVO<>();
        Page<Emp> page = new Page<>();
        page.setSize(pageEmpVO.getPageSize());
        page.setCurrent(pageEmpVO.getPageNum());
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        Integer informal = pageEmpVO.getInformal();
        Integer status = pageEmpVO.getStatus();
        if (informal != null) {
            queryWrapper.eq("informal",informal);
        }
        if (status != null) {
            queryWrapper.eq("status",status);
        }
        queryWrapper.orderByDesc("id");
        this.page(page,queryWrapper);
        List<Emp> records = page.getRecords();
        List<PageEmpDataVO> dataVOS = new ArrayList<>();
        for (Emp emp : records) {
            PageEmpDataVO pageEmpDataVO = new PageEmpDataVO();
            BeanUtil.copyProperties(emp,pageEmpDataVO);
            dataVOS.add(pageEmpDataVO);
        }
        pageVO.setItems(dataVOS);
        pageVO.setPageNum(page.getCurrent());
        pageVO.setPageSize(page.getSize());
        pageVO.setTotalItems(page.getTotal());
        pageVO.setTotalPages(page.getPages());
        return pageVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addEmp(AddEmpVO addEmpVO) {
        Emp emp = new Emp();
        emp.setName(addEmpVO.getName());
        emp.setAge(addEmpVO.getAge());
        emp.setGender(addEmpVO.getGender());
        emp.setPhone(addEmpVO.getPhone());
        emp.setCityId(addEmpVO.getCityId());
        emp.setDepId(addEmpVO.getDepId());
        emp.setIdNumber(addEmpVO.getIdNumber());
        emp.setPapers(addEmpVO.getPapers());
        emp.setEntryTime(addEmpVO.getEntryTime());
        emp.setAddress(addEmpVO.getAddress());
        emp.setJob(addEmpVO.getJob());
        emp.setStatus(addEmpVO.getStatus());
        emp.setHire(addEmpVO.getHire());
        // hire == 1 正式员工，== 2 非正式
        if (addEmpVO.getHire() == 1) {
            //正式员工设置实习期 按月算
            emp.setPro(addEmpVO.getPro());
        }
        if (addEmpVO.getHire() == 2) {
            //非正式员工设置非正式的类型 1：实习，2：劳务，3：顾问，4：返聘，5：外包，6：兼职
            emp.setInformal(addEmpVO.getInformal());
        }
        boolean save = this.save(emp);
        if (!save){
            throw new ApiException("新增失败");
        }
        //新增薪资
        Salary salary = new Salary();
        salary.setEmpId(emp.getId());
        salary.setDepId(emp.getDepId());
        salary.setJob(emp.getJob());
        salaryService.saveSalary(salary);
    }

    @Override
    public void updateStatus(Integer empId) {
        Emp emp = this.getById(empId);
        if (emp == null) {
            throw new ApiException("没有该员工");
        }
        if (emp.getStatus() == 2) {
            throw new ApiException("他已经是正式员工");
        }
        emp.setStatus(1);
        this.updateById(emp);
    }

    @Override
    public void updateDep(Integer empId,Integer depId) {
        Emp emp = this.getById(empId);
        if (emp == null) {
            throw new ApiException("没有该员工");
        }
        emp.setDepId(depId);
        boolean byId = this.updateById(emp);
        if (!byId) {
            throw new ApiException("修改失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeDep(List<Integer> idsList) {
        this.removeByIds(idsList);
        //根据id查工号
        //拿员工工号删对应薪资
        salaryService.removeByIds(idsList);
    }
}
